package gold.digger;

import gold.utils.InputUtil;
import gold.vo.TreeNode;

import java.util.LinkedList;
import java.util.Queue;

/**
 * Created by fanzhenyu02 on 2021/12/10.
 * common problem solver template.
 */
public class LC1161 {
    public long startExecuteTime = System.currentTimeMillis();


    class Solution {
        public int maxLevelSum(TreeNode root) {
            Queue<TreeNode> queue = new LinkedList<>();
            int maxSum = Integer.MIN_VALUE, levelNumber = 1, curLevel = 1;
            queue.offer(root);

            while (!queue.isEmpty()) {
                int size = queue.size(), sum = 0;
                for (int i = 0; i < size; i++) {
                    sum += queue.peek().val;
                    if (null != queue.peek().left) queue.offer(queue.peek().left);
                    if (null != queue.peek().right) queue.offer(queue.peek().right);
                    queue.poll();
                }

                if (sum > maxSum) {
                    levelNumber = curLevel;
                    maxSum = sum;
                }
                curLevel++;
            }

            return levelNumber;
        }
    }

    public void run() {
        int[] arr = InputUtil.toIntegerArray("[1,2,3]");
        System.out.println(new Solution().toString());
    }

    public static void main(String[] args) throws Exception {
        LC1161 an = new LC1161();
        an.run();

        System.out.println("\ncurrent solution total execute time: " + (System.currentTimeMillis() - an.startExecuteTime) + " ms.");
    }
}
